#include "stdafx.h"

#include <iostream>
#include "CinePlugin.h"
#include "resource.h"
#include "CmdRunner.h"
#include "CmdRunnerDialog.h"

CWinApp app;

CmdRunner::CmdRunner()
{
    facade = NULL;
//     dlg = NULL;
}

CmdRunner::~CmdRunner()
{
//     delete dlg;
}

void CmdRunner::Initialize( IPluginFacade* cine )
{
    facade = cine;
}

void CmdRunner::Activate()
{
    cmd = facade->RegisterCommand( this );
    CMenu& menu = facade->GetWindowMenu();
    menu.AppendMenu( MF_STRING, cmd, TEXT("CMD Runner...") );
    facade->AttachListener( Events::Command , this );
    facade->AttachListener( Events::UpdateCommandUI , this );
	facade->RegisterAccelKey("Plugins/Config/CmdRunner.xml","cmd_runner",cmd);
//     dlg = new CmdRunnerDialog;
//     UINT ret = dlg->Create( (UINT)IDD_CMDPROMPT_DLG, NULL/*this*/ );
//     std::cerr << GetLastError() << ' ' << dlg << ' ' << ret << '\n';
}

void CmdRunner::Deactivate()
{
    CMenu& menu = facade->GetWindowMenu();
    facade->ReleaseListener( this );
    menu.RemoveMenu( cmd, MF_BYCOMMAND );
    facade->ReleaseCommand( cmd );

//     delete dlg;
//     dlg = NULL;
}

bool CmdRunner::HandleEvent( Events::Event* e )
{
    AFX_MANAGE_STATE(AfxGetStaticModuleState());
    switch ( e->GetType() ) {
        case Events::Command: {
            Events::CommandEvent* ce = (Events::CommandEvent*)e;
            if ( ce->cmd == cmd ) {
                std::cerr << "show dialog box\n";
                CmdRunnerDialog dlg2;
                dlg2.DoModal();
                std::cerr << GetLastError() << '\n';
            }
            break;
        }
        case Events::UpdateCommandUI:
            break;
    }
    return false;
}

void CmdRunner::Release()
{
}

IPlugin* CreatePluginObject()
{
    return new CmdRunner;
}

void DestroyPluginObject(IPlugin * plugin)
{
    delete (CmdRunner*)plugin;
}

const char* GetPluginName()
{
    return "CmdRunner";
}

int GetPluginVersion()
{
    return 0;
}
